<template>
  <layout>
    <template slot="title" v-if="!isPublish">
      <span>
        种粮大户补贴申报信息列表
      </span>
      <template>
        <a-button
          type="primary"
          style="margin-left: 20px;margin-right: 20px;"
          auth="large-member.add"
          @click="handlerTableAdd()"
        >添加大户档案</a-button>
        <a-button
          key="submit"
          type="primary"
          style="margin-right: 20px;"
          @click="handlerPreview({big_mb_id: ''})"
        >
          打印空白申请表
        </a-button>
        <a-button-group>
          <a-button
            type="primary"
            icon="cloud-upload"
            @click="$refs.GrainImport.handlerUploadExecl()"
          >导入原单机版申报信息</a-button>
          <a-button
            type="default"
            icon="download"
            style="margin-right: 20px;"
            @click="handlerDownload($downloadLargeMemberTemplate)"
          >
            下载申报信息模板
          </a-button>
        </a-button-group>

        <a-button
          type="dashed"
          icon="question-circle"
          @click="events.trigger('show-helper')"
        >
          帮助
        </a-button>
      </template>
    </template>

    <!-- 顶部操作 -->
    <section slot="search">
      <custom-form
        ref="search-form"
        class="search-form"
        v-model="tableSearch"
        layout="inline"
        mode="search"
        :FormList="searchColumns"
      >
        <a-button
          class="action-button"
          type="primary"
          @click="search()"
        >搜索</a-button>
      </custom-form>
    </section>

    <!-- 产品列表 -->
    <a-table
      :dataSource="tableData"
      :columns="tableColumns"
      :rowKey="record => record.usr_id"
      :customRow="customTableRow"
      :pagination="false"
      :loading="tableLoading"
      class="table-total"
      :scroll="{
        x: 'max-content',
        y: tableClientHeight
      }"
      bordered
      @change="handleTableChange"
    >
      
      <template
        slot="apl_bh"
        slot-scope="row"
      >
        {{row.apl_bh}}
      </template>
      <template
        slot="big_mb_ck1"
        slot-scope="row"
      >
        {{getLabel(row.big_mb_ck1, [], 'big_mb_ck1')}}
      </template>
      <template
        slot="big_mb_ck2"
        slot-scope="row"
      >
        {{getLabel(row.big_mb_ck2, [], 'big_mb_ck2')}}
      </template>
      <template
        slot="big_mb_ck3"
        slot-scope="row"
      >
        {{getLabel(row.big_mb_ck3, [], 'big_mb_ck3')}}
      </template>
      <template
        slot="big_mb_type"
        slot-scope="row"
      >
        {{getLabel(row.big_mb_type, [], 'big_mb_type')}}
      </template>
      <template
        slot="big_mb_bank_code"
        slot-scope="row"
      >
        {{getLabel(row.big_mb_bank_code, [], 'big_mb_bank_code')}}
      </template>
      <template
        slot="pay_unit"
        slot-scope="row"
      >
        {{getLabel(row.pay_unit, [], 'pay_unit')}}
      </template>
      <template
        slot="field"
        slot-scope="row"
      >
        {{row.field || 0}}
      </template>
      <template
        slot="field1"
        slot-scope="row, record, index"
      >
        <span v-if="index === 0">{{row.field1 || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.field-list', 'publish.member-list']"
          showDefaultLabel
          @click="handlerManageFarmLand(row, 1)"
        >{{row.field1 || 0}}</a-button>
      </template>
      <template
        slot="field2"
        slot-scope="row, record, index"
      >
        <span v-if="index === 0">{{row.field2 || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.field-list', 'publish.member-list']"
          showDefaultLabel
          @click="handlerManageFarmLand(row, 2)"
        >{{row.field2 || 0}}</a-button>
      </template>
      <template
        slot="field3"
        slot-scope="row, record, index"
        
      >
        <span v-if="index === 0">{{row.field3 || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.field-list', 'publish.member-list']"
          showDefaultLabel
          @click="handlerManageFarmLand(row, 3)"
        >{{row.field3 || 0}}</a-button>
      </template>
      <template
        slot="plant"
        slot-scope="row, record, index"
      >
        <span v-if="index === 0">{{row.plant || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.edit', 'publish.member-list']"
          showDefaultLabel
          @click="handleTableEdit(row, 'plant')"
        >{{row.plant || 0}}</a-button>
      </template>
      <template
        slot="material"
        slot-scope="row, record, index"
      >
        <span v-if="index === 0">{{row.material || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.edit', 'publish.member-list']"
          showDefaultLabel
          @click="handleTableEdit(row, 'material')"
        >{{row.material || 0}}</a-button>
      </template>
      <template
        slot="machine"
        slot-scope="row, record, index"
      >
        <span v-if="index === 0">{{row.machine || 0}}</span>
        <a-button
          v-else
          type="link"
          :auth="['large-member.edit', 'publish.member-list']"
          showDefaultLabel
          @click="handleTableEdit(row, 'machine')"
        >{{row.machine || 0}}</a-button>
      </template>
      <!-- 按钮区 -->
      <template
        slot="action"
        slot-scope="row, record, index"
        v-if="index !== 0"
      >
        <a-button
          class="action-button"
          type="link"
          size="small"
          auth="large-member.edit"
          @click="handleTableEdit(row, 'edit')"
        >编辑</a-button>
        <a-popconfirm
          title="您确认要删除该条数据?"
          ok-text="确认"
          cancel-text="取消"
          @confirm="tableDel(row)"
        >
          <a-button
            type="link"
            auth="large-member.delete"
            size="small"
          >删除</a-button>
        </a-popconfirm>
        <a-button
          class="action-button"
          type="link"
          size="small"
          auth="large-member.auth"
          @click="handleTableEdit(row, 'review')"
        >审核</a-button>
        <a-button
          class="action-button"
          type="link"
          size="small"
          auth="large-member.preview"
          @click="handlerPreview(row)"
        >预览</a-button>
      </template>
    </a-table>

    <!-- 产品列表分页器 -->
    <a-pagination
      slot="paging"
      :pageSizeOptions="pageSizes"
      :total="total"
      showSizeChanger
      :show-total="showTotal"
      :pageSize="pageSize"
      v-model="currentPage"
      @showSizeChange="changePageSize"
      @change="changePage"
    >
    </a-pagination>

    <!-- 编辑产品数据 -->
    <a-modal
      :visible="visible"
      :title="visibleTitle"
      centered
      :maskClosable="false"
      :width="560"
      v-bind="isPublish ? {footer: null} : {}"
      @cancel="visible = false"
      @ok="handleSubmit"
    >
      <custom-form
        ref="custom-form"
        class="custom-form"
        :class="{ 'review-form': customMode === 'review' }"
        v-model="currentRow"
        :FormList="formColumns"
        :mode="customMode"
        v-bind="formItemLayout"
        layout="horizontal"
      >
        <template slot="big_mb_ck1" slot-scope="{params, column}">
          <a-input v-model="params.big_mb_ck1" v-bind="column.form">
            <!-- <a-icon slot="addonAfter" type="setting" /> -->
            <span slot="addonAfter" style="cursor: pointer" @click="handlerCheck(params)">核查计算</span>
          </a-input>
        </template>

      </custom-form>
    </a-modal>

    <!-- 管理耕地信息 -->
    <a-drawer
      :title="currentRow.big_mb_nam + '-耕地管理'"
      placement="right"
      :closable="false"
      width="80%"
      :visible="drawerVisible"
      @close="drawerVisible = false"
    >
      <Farmland
        :currentField="currentRow"
        :currentFieldType="currentFieldType"
        :isReadOnly="Boolean(currentPublishId)"
        :currentPublishId="currentPublishId"
      />
    </a-drawer>

    <a-modal
      :visible="subsidyVisible"
      :title="`${currentRow.big_mb_nam ? currentRow.big_mb_nam + '-' : ''}` + '种粮大户补贴申报表'"
      centered
      :width="840"
      @cancel="subsidyVisible = false"
    >
      <Preview :currentRow="currentRow" />
      <template slot="footer">
        <a-popconfirm
          title="您确认要删除申报数据?"
          ok-text="确认"
          cancel-text="取消"
          v-if="currentRow.big_mb_id"
          @confirm="handlerDeleteApply()"
        >
          <a-button
            key="del"
          >
            删除申报数据
          </a-button>
        </a-popconfirm>
        <a-button
          key="back"
          @click="subsidyVisible = false"
        >
          取消
        </a-button>
        <a-button
          key="submit"
          type="primary"
          v-print="{ id: '#subsidy-table', popTitle: settingModule.systemName }"
        >
          打印
        </a-button>
      </template>
    </a-modal>

    <GrainImport ref="GrainImport" />

  </layout>
</template>

<script lang="ts">
import { Mixins, Component, Watch, Vue, Prop } from 'vue-property-decorator'
import { State, Mutation, Getter, namespace } from 'vuex-class'
import EventsMixin from '@/mixin/events'
import { TableMixin, AuthMixin } from '@/mixin'

import { TableLayout } from '@/layouts'
import CustomForm from '@/components/src/Form'
import {
  largeMemberServices,
  ILargeMemberDetail
} from '@/common/services/largeMember'
import { publishServices } from '@/common/services/publish'
import { commonServices } from '@/common/services/common'
import { memberRole, LargeMemberType, reviewDict, PayUnitType, PayUnitTypeEnum } from '@/common/enum'
import Farmland from './Farmland.vue'
import Preview from './Preview.vue'
import GrainImport from './GrainImport.vue'
import { userModule } from '@/store/modules/user'
import { settingModule } from '@/store/modules/setting'

import { merge } from 'lodash-es'

@Component({
  components: {
    Layout: TableLayout,
    CustomForm,
    Farmland,
    Preview,
    GrainImport
  }
})
export default class AdminList extends Mixins(
  EventsMixin,
  TableMixin,
  AuthMixin
) {
  @Prop({
    type: String
  })
  public currentAreaId!: string

  @Prop({
    type: String
  })
  public currentPublishId!: string

  get isPublish() {
    return Boolean(this.currentPublishId)
  }

  get currentUserArea() {
    return userModule.currentUserArea
  }

  get settingModule() {
    return settingModule
  }

  public commonParams = {
    s_a_id: '',
    pub_id: this.currentPublishId
  }

  // 产品列表搜索条件
  public tableSearch = {
    show_area: '0'
  }

  // 自定义mode
  public customMode = 'add'

  public formItemLayout = {
    labelCol: { span: 6 },
    wrapperCol: { span: 18 }
  }

  // 审核功能字段
  get reviewFields () {
    const len = this.currentUserArea.s_a_id.length
    const reviews = [
      {
        title: '区县核定补贴面积（亩）',
        width: 120,
        dataIndex: 'big_mb_ck',
        key: 'big_mb_ck',
        align: 'right',
        form: {
          type: 'a-input',
          placeholder: '请输入区县核定补贴面积',
          renderMode: ['review'],
          len: 6
        }
      },
      {
        title: '区县负责人姓名',
        dataIndex: 'big_mb_ck_u',
        key: 'big_mb_ck_u',
        width: 120,
        form: {
          type: 'a-input',
          placeholder: '请输入区县负责人姓名',
          renderMode: ['review'],
          len: 6
        }
      },
      {
        title: '区县核定时间',
        dataIndex: 'big_mb_time',
        key: 'big_mb_time',
        width: 150,
        form: {
          type: 'a-date-picker',
          placeholder: '请选择区县核定时间',
          showTime: { format: 'HH:mm' },
          format: 'YYYY-MM-DD HH:mm',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          renderMode: ['review'],
          len: 6
        }
      },
      {
        title: '乡镇核查面积（亩）',
        // scopedSlots: { customRender: 'big_mb_ck1' },
        dataIndex: 'big_mb_ck1',
        width: 120,
        key: 'big_mb_ck1',
        align: 'right',
        form: {
          type: 'a-input',
          placeholder: '请输入乡镇核查面积',
          renderMode: ['review'],
          len: 9,
          slot: 'big_mb_ck1'
          // dict: reviewDict
        }
      },
      {
        title: '乡镇负责人姓名',
        dataIndex: 'big_mb_ck_u1',
        key: 'big_mb_ck_u1',
        width: 120,
        form: {
          type: 'a-input',
          placeholder: '请输入乡负责人姓名',
          renderMode: ['review'],
          len: 9
        }
      },
      {
        title: '乡镇审核时间',
        dataIndex: 'big_mb_time1',
        key: 'big_mb_time1',
        width: 150,
        form: {
          type: 'a-date-picker',
          placeholder: '请选择乡镇审核时间',
          showTime: { format: 'HH:mm' },
          format: 'YYYY-MM-DD HH:mm',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          renderMode: ['review'],
          len: 9
        }
      },
      {
        title: '村委审核意见',
        scopedSlots: { customRender: 'big_mb_ck2' },
        key: 'big_mb_ck2',
        width: 120,
        form: {
          type: 'a-radio-group',
          placeholder: '请选择村委审核意见',
          renderMode: ['review'],
          dict: reviewDict,
          len: 12
        }
      },
      {
        title: '村委审核人姓名',
        dataIndex: 'big_mb_ck_u2',
        key: 'big_mb_ck_u2',
        width: 120,
        form: {
          type: 'a-input',
          placeholder: '请输入村委审核人姓名',
          renderMode: ['review'],
          len: 12
        }
      },
      {
        title: '村委审核时间',
        dataIndex: 'big_mb_time2',
        key: 'big_mb_time2',
        width: 150,
        form: {
          type: 'a-date-picker',
          placeholder: '请选择乡镇审核时间',
          showTime: { format: 'HH:mm' },
          format: 'YYYY-MM-DD HH:mm',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          renderMode: ['review'],
          len: 12
        }
      },
      {
        title: '驻村干部审核意见',
        scopedSlots: { customRender: 'big_mb_ck3' },
        key: 'big_mb_ck3',
        width: 140,
        form: {
          type: 'a-radio-group',
          placeholder: '请选择驻村干部审核意见',
          renderMode: ['review'],
          dict: reviewDict,
          len: 12
        }
      },
      {
        title: '驻村干部审核人姓名',
        dataIndex: 'big_mb_ck_u3',
        key: 'big_mb_ck_u3',
        width: 150,
        form: {
          type: 'a-input',
          placeholder: '请输入驻村干部审核人姓名',
          renderMode: ['review'],
          len: 12
        }
      },
      {
        title: '驻村干部审核时间',
        dataIndex: 'big_mb_time3',
        key: 'big_mb_time3',
        width: 160,
        form: {
          type: 'a-date-picker',
          placeholder: '请选择乡镇审核时间',
          showTime: { format: 'HH:mm' },
          format: 'YYYY-MM-DD HH:mm',
          valueFormat: 'YYYY-MM-DD HH:mm:ss',
          renderMode: ['review'],
          len: 12
        }
      }
    ]
    return reviews.map(item => {
      const disabled = (item?.form?.len || 0) < len
      return merge(item, {
        form: {
          disabled: disabled
        }
      })
    })
  }

  // 产品列表表格信息
  public columns: ITable.IColumn[] = [
    {
      title: '申报编号',
      // dataIndex: 'apl_bh',
      key: 'apl_bh',
      width: 180,
      scopedSlots: { customRender: 'apl_bh' },
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        isShowFrom: false
      }
    },
    {
      title: '受理行政区',
      dataIndex: 's_a_nam_all',
      key: 's_a_nam_all',
      width: 180,
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        isShowFrom: false
      }
    },
    {
      title: '大户编号',
      dataIndex: 'big_mb_id',
      key: 'big_mb_id',
      width: 120,
      form: {
        type: 'a-input',
        placeholder: '编号自动生成',
        disabled: true,
        renderMode: ['edit']
      }
    },
    {
      title: '查询范围',
      key: 'show_area',
      dataIndex: 'show_area',
      width: 80,
      form: {
        type: 'a-radio-group',
        placeholder: '请选择大户类型',
        dict: [
          {
            label: '本区域申报的',
            value: '0'
          },
          {
            label: '本区域种植的',
            value: '1'
          }
        ],
        isShowQuery: true,
        isShowList: false,
        isShowAll: false,
        renderMode: ['search']
      }
    },
    {
      title: '大户类型',
      key: 'big_mb_type',
      width: 80,
      scopedSlots: { customRender: 'big_mb_type' },
      form: {
        type: 'a-radio-group',
        placeholder: '请选择大户类型',
        dict: LargeMemberType,
        isShowQuery: true,
        validate: '*',
        renderMode: ['search', 'edit', 'add'],
        on: {
          // change: (...e) => {
          //   console.log(e)
          // }
        }
      }
    },
    {
      title: '大户名称',
      dataIndex: 'big_mb_nam',
      key: 'big_mb_nam',
      width: 160,
      form: {
        type: 'a-input',
        placeholder: '请输入大户名称',
        isShowQuery: true,
        renderMode: ['search', 'edit', 'add']
      }
    },
    {
      title: '法定编码',
      dataIndex: 'big_mb_law_id',
      width: 180,
      key: 'big_mb_law_id',
      form: {
        type: 'a-input',
        placeholder: '请输入农户编码或企业社会信用代码',
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '种粮地点',
      dataIndex: 'big_mb_addr',
      key: 'big_mb_addr',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入种粮地点',
        renderMode: ['edit', 'add']
      }
    },
    
    {
      title: '负责人姓名',
      dataIndex: 'big_mb_user',
      width: 100,
      key: 'big_mb_user',
      form: {
        type: 'a-input',
        placeholder: '请输入负责人姓名',
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '负责人身份证',
      dataIndex: 'big_mb_sfz',
      width: 160,
      key: 'big_mb_sfz',
      form: {
        type: 'a-input',
        placeholder: '请输入负责人身份证',
        renderMode: ['edit', 'add']
      }
    },
    // {
    //   title: '联系人',
    //   dataIndex: 'big_mb_contact',
    //   key: 'big_mb_contact',
    //   width: 80,
    //   form: {
    //     type: 'a-input',
    //     placeholder: '请输入联系人',
    //     renderMode: ['edit', 'add']
    //   }
    // },
    {
      title: '联系电话',
      dataIndex: 'big_mb_tel',
      key: 'big_mb_tel',
      width: 110,
      form: {
        type: 'a-input',
        placeholder: '请输入联系电话',
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '银行账号',
      dataIndex: 'big_mb_bank_id',
      key: 'big_mb_bank_id',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入银行账号',
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '银行户名',
      dataIndex: 'big_mb_bank_user',
      key: 'big_mb_bank_user',
      width: 150,
      form: {
        type: 'a-input',
        placeholder: '请输入银行户名',
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '开户行',
      key: 'big_mb_bank_code',
      width: 150,
      scopedSlots: { customRender: 'big_mb_bank_code' },
      form: {
        type: 'a-auto-complete',
        placeholder: '请选择/输入开户银行',
        showSearch: true,
        dataSource: [],
        renderMode: ['edit', 'add']
      }
    },
    {
      title: '确权面积（亩）',
      width: 300,
      children: [
        {
          title: '合计',
          scopedSlots: { customRender: 'field' },
          key: 'field',
          width: 110,
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland']
          }
        },
        {
          title: '承包地',
          scopedSlots: { customRender: 'field1' },
          key: 'field1',
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          width: 110,
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland'],
            isShowQuery: true,
            querySection: true
          }
        },
        {
          title: '增加面积',
          scopedSlots: { customRender: 'field2' },
          key: 'field2',
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          width: 110,
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland'],
            isShowQuery: true,
            querySection: true
          }
        },
        {
          title: '减少面积',
          scopedSlots: { customRender: 'field3' },
          key: 'field3',
          sorter: true,
          sortDirections: ['ascend', 'descend'],
          width: 110,
          form: {
            type: 'a-input',
            placeholder: '请输入耕地总面积',
            renderMode: ['farmland'],
            isShowQuery: true,
            querySection: true
          }
        },
      ]
    },
    // {
    //   title: '耕地总面积',
    //   scopedSlots: { customRender: 'field' },
    //   key: 'field',
    //   width: 140
    // },
    // {
    //   title: '承包确权面积(亩)',
    //   scopedSlots: { customRender: 'field_had' },
    //   key: 'field_had',
    //   width: 180
    // },
    // {
    //   title: '租用确权面积(亩)',
    //   scopedSlots: { customRender: 'field_gave' },
    //   key: 'field_gave',
    //   width: 180
    // },
    
    {
      title: '粮食种植面积(亩)',
      key: 'plant',
      width: 120,
      sorter: true,
      sortDirections: ['ascend', 'descend'],
      align: 'center',
      scopedSlots: { customRender: 'plant' },
      form: {
        type: 'a-input',
        isShowQuery: true,
        querySection: true,
        isShowFrom: false
      }
    },
    {
      title: '购买农资花费(元)',
      key: 'material',
      width: 120,
      align: 'center',
      scopedSlots: { customRender: 'material' }
    },
    {
      title: '农机数量(台)',
      key: 'machine',
      width: 80,
      align: 'center',
      scopedSlots: { customRender: 'machine' }
    },
    {
      title: '申报时间',
      key: 'apl_time',
      dataIndex: 'apl_time',
      width: 150,
      align: 'center',
    },
    
    // 审核字段
    ...this.reviewFields as ITable.IColumn[]
  ]

  // 金额相关字段
  public moneyFields: ITable.IColumn[] = [
    {
      title: '补贴标准（亩/元）',
      dataIndex: 'pay_price',
      key: 'pay_price',
      width: 150,
    },
    {
      title: '补贴核算依据',
      scopedSlots: { customRender: 'pay_unit' },
      key: 'pay_unit',
      width: 150,
      form: {
        type: 'a-select',
        placeholder: '请选择补贴参照',
        isShowFrom: false,
        dict: PayUnitType
      }
    },
    {
      title: '补贴总金额（元）',
      dataIndex: 'pay_total',
      key: 'pay_total',
      width: 150,
    }
  ]

  public tableApi = {
    list: this.currentPublishId
      ? publishServices.getPublishUsers
      : largeMemberServices.getLargeMemberDetail,
    add: largeMemberServices.updateLargeMember,
    edit: largeMemberServices.updateLargeMember,
    del: largeMemberServices.deleteLargeMember
    // imageUpload: 'uploadStoreImage',
    // imageRemove: 'delStoreImage'
  }

  @Watch('currentAreaId')
  @Watch('currentPublishId', { immediate: true })
  public handlerCurrentAreaIdChange() {
    // 公示页面进入
    if (this.currentPublishId) {
      this.commonParams.pub_id = this.currentPublishId
    } else {
      if (!this.currentAreaId) return
      this.commonParams.s_a_id = this.currentAreaId
    }
    this.getData()
  }

  public mounted() {
    this.handlerAddGrainColumn()
    this.handlerAddMaterialColumn()
    this.handlerAddMachineColumn()
    // 公示模式下不提供操作功能
    if (!this.isPublish) {
      this.columns.push({
        title: '操作',
        key: 'action',
        width: 340,
        fixed: 'right',
        scopedSlots: { customRender: 'action' }
      })
    } else {
      this.columns.push(...this.moneyFields)
    }
    
    this.getBankTypes()
  }

  public handlerAddGrainColumn() {
    const grains = [
      '水稻',
      '玉米',
      '小麦',
      '红苕',
      '马铃薯',
      '大豆',
      '绿豆',
      '豌葫豆',
      '高粱',
      '荞麦',
      '肾豆',
      '红小豆'
    ]
    const grainsColumns = grains.map((item, index) => {
      const key = 'plant' + (index + 1)
      return {
        title: item,
        dataIndex: key,
        key,
        form: {
          type: 'a-input',
          placeholder: `请输入${item}面积`,
          renderMode: ['plant'],
          isShowList: false
        }
      }
    })
    this.columns = this.columns.concat(grainsColumns)
  }

  public handlerAddMaterialColumn() {
    const grains = [
      '购买种子花费(元)',
      '购买肥料花费(元)',
      '购买农膜花费(元)',
      '购买农药花费(元)'
    ]
    const grainsColumns = grains.map((item, index) => {
      const key = 'material' + (index + 1)
      return {
        title: item,
        dataIndex: key,
        key,
        form: {
          type: 'a-input',
          placeholder: `请输入${item}花费`,
          renderMode: ['material'],
          isShowList: false
        }
      }
    })
    this.columns = this.columns.concat(grainsColumns)
  }

  public handlerAddMachineColumn() {
    const grains = ['微耕机(台)', '插秧机(台)', '收割机(台)', '灌溉设备(台)']
    const grainsColumns = grains.map((item, index) => {
      const key = 'machine' + (index + 1)
      return {
        title: item,
        dataIndex: key,
        key,
        form: {
          type: 'a-input',
          placeholder: `请输入${item}台数`,
          renderMode: ['machine'],
          isShowList: false
        }
      }
    })
    this.columns = this.columns.concat(grainsColumns)
  }

  public async getData() {
    // @fix 修复直接刷新页面时没有s_a_id 接口异常问题
    if (!this.currentPublishId && !this.commonParams.s_a_id) return
    const res = await this.getTableList()
  }

  public getBankTypes() {
    commonServices.getBankType().then(res => {
      const dict = []
      this.changeDict(
        'big_mb_bank_code',
        Object.keys(res.data).map(key => res.data[key]),
        'dataSource'
      )
    })
  }

  public handlerTableAdd() {
    this.customMode = "add"
    this.tableAdd()
    this.$nextTick(() => {
      this.currentRow = Object.assign({}, {
        big_mb_type: '1'
      })
    })
    this.visibleTitle = "添加大户基本信息"
  }

  // 编辑前数据预处理
  public async handleTableEdit(row: ILargeMemberDetail, mode: string) {
    this.customMode = mode
    let methods
    switch (mode) {
      // 种粮
      case 'plant':
        // @ts-ignore
        this.tableApi.edit = largeMemberServices.updateLargeMemberPlant
        methods = this.currentPublishId
          ? publishServices.getPublishLargePlants
          : largeMemberServices.getLargeMemberPlants
        methods({
          big_mb_id: row.big_mb_id,
          s_a_id: row.s_a_id,
          // @ts-ignore
          pub_id: this.currentPublishId
        }).then(res => {
          this.tableEdit(Object.assign({}, row, (res.data || {})))
          if (!res.data) {
            this.$nextTick(() => {
              this.$refs['custom-form'].resetFields()
            })
          }
          this.visibleTitle = "填写粮食种植面积(亩)"
          this.formItemLayout =  {
            labelCol: { span: 4 },
            wrapperCol: { span: 20 }
          }
        })
        break
      // 农资
      case 'material':
        // @ts-ignore
        this.tableApi.edit = largeMemberServices.updateLargeMemberMaterial
        methods = this.currentPublishId
          ? publishServices.getPublishLargeMemberMaterial
          : largeMemberServices.getLargeMemberMaterial
        methods({
          big_mb_id: row.big_mb_id,
          s_a_id: row.s_a_id,
          pub_id: this.currentPublishId
        }).then(res => {
          this.tableEdit(Object.assign({}, row, res.data))
          if (!res.data) {
            this.$nextTick(() => {
              this.$refs['custom-form'].resetFields()
            })
          }
          this.visibleTitle = "填写购买农资花费"
        })
        break
      // 农机
      case 'machine':
        // @ts-ignore
        this.tableApi.edit = largeMemberServices.updateLargeMemberMachines
        methods = this.currentPublishId
          ? publishServices.getPublishLargeMemberMachines
          : largeMemberServices.getLargeMemberMachines
        methods({
          big_mb_id: row.big_mb_id,
          s_a_id: row.s_a_id,
          pub_id: this.currentPublishId
        }).then(res => {
          this.tableEdit(Object.assign({}, row, res.data))
          if (!res.data) {
            this.$nextTick(() => {
              this.$refs['custom-form'].resetFields()
            })
          }
          this.visibleTitle = "填写农机数量"
        })
        break
      // 审核
      case 'review':
        // @ts-ignore
        this.tableApi.edit = largeMemberServices.checkLargeMember
        let defaultParams: Partial<ILargeMemberDetail> = {}
        if (!row.big_mb_ck3) {
          defaultParams.big_mb_ck3 = '1'
        }
        if (!row.big_mb_ck2) {
          defaultParams.big_mb_ck2 = '1'
        }
        this.tableEdit({...row, ...defaultParams})
        this.visibleTitle = "补贴审核意见"
        this.formItemLayout =  {
          labelCol: { span: 9 },
          wrapperCol: { span: 15 }
        }
        break
      default:
        this.tableApi.edit = largeMemberServices.updateLargeMember
        this.tableEdit({...row})
        this.visibleTitle = "编辑大户基础信息"
        this.formItemLayout =  {
          labelCol: { span: 6 },
          wrapperCol: { span: 18 }
        }
    }
  }

  public drawerVisible = false
  public currentFieldType = 1
  // 管理耕地信息
  public handlerManageFarmLand(row: ILargeMemberDetail, mode: number) {
    this.currentRow = Object.assign({}, row)
    this.currentFieldType = mode
    this.drawerVisible = true
  }

  // 预览补贴申请表
  public subsidyVisible = false
  public handlerPreview(row: Partial<ILargeMemberDetail>) {
    this.currentRow = Object.assign({}, row)
    this.subsidyVisible = true
  }

  // 删除申报数据
  public handlerDeleteApply() {
    largeMemberServices.deleteLargeMember({
      big_mb_id: this.currentRow.big_mb_id,
      s_a_id: this.currentAreaId,
      all: '0'
    }).then(_ => {
      this.subsidyVisible = false
      this.$message.success('申报数据删除成功')
      this.getData()
    })
  }

  // 下载
  public handlerDownload(link: string) {
    const a = document.createElement('a')
    a.href = link
    a.target = '_blank'
    a.click();
  }

  // 核查
  public handlerCheck(row: ILargeMemberDetail) {
    const temp = this.currentRow.pay_unit === PayUnitTypeEnum.FIELD ? this.currentRow.field : this.currentRow.plant
    if (!this.currentRow.big_mb_ck1) {
      this.currentRow.big_mb_ck1 = temp
    }
    // 不相等
    if (this.currentRow.big_mb_ck1 !== temp) {
      this.$confirm({
        title: '提示',
        content: '核算面积和当前输入面积不相等，确认覆盖吗？',
        onOk: () => {
          this.currentRow.big_mb_ck1 = temp
        }
      })
    }
  }
}
</script>

<style scoped lang="less">
.action-button {
  margin: 0px 10px 0px 10px;
}
.form-item-class {
  margin-bottom: 0px;
}
.img-box {
  width: 100px;
  height: 100px;
  margin: 10px;
}
.item-blue {
  color: #1989fa;
}

/deep/ .ant-table-thead > tr > th, .ant-table-tbody > tr > td {
  padding: 16px 16px;
  word-break: break-word;
  -ms-word-break: break-all;
}

// 审核表专用样式
/deep/ .review-form {
  .ant-form-item {
    &:nth-child(3), &:nth-child(6) {
      &::after {
        clear: both;
        display: table;
        content: '';
        width: 100%;
        height: 1px;
        border-bottom: 1px solid #ddd;
        padding-top: 10px;
      }
    }
  }
}
</style>
